package com.gy.api.pc.content.service.impl;

import com.gy.api.base.Type;
import com.gy.api.pc.content.dao.ArticleInfoDao;
import com.gy.api.pc.content.domain.po.ArticleInfoPO;
import com.gy.api.pc.content.domain.po.PosterPO;
import com.gy.api.pc.content.domain.vo.*;
import com.gy.api.pc.content.service.IArticleInfoService;
import com.gy.api.pc.content.service.IBannerService;
import com.gy.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class ArticleInfoServiceImpl implements IArticleInfoService {

    @Autowired
    ArticleInfoDao articleInfoDao;

    @Override
    public void delete(List<String> ids) {
        for (String id:ids) {
            ArticleInfoPO posterPO=articleInfoDao.selectByPrimaryKey(id);
            if(posterPO==null){
                throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
            }
            posterPO.preDelete();
            articleInfoDao.updateByPrimaryKeySelective(posterPO);
        }
    }

    @Override
    public List<ArticleInfoVO> pageCulvert(ArticleSearchDto dto) {
        return articleInfoDao.pageCulvert(dto);
    }

    @Override
    public void save(ArticleInfoAddVO addDto) {
        ArticleInfoPO articleInfoPO=new ArticleInfoPO();
        BeanUtils.copyProperties(addDto,articleInfoPO);
        articleInfoPO.preInsert();
        articleInfoDao.insertSelective(articleInfoPO);
    }

    @Override
    public void update(ArticleInfoUpdateVO addDto) {
        ArticleInfoPO posterPO=articleInfoDao.selectByPrimaryKey(addDto.getId());
        if(posterPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        BeanUtils.copyProperties(addDto,posterPO);
        posterPO.preUpdate();
        articleInfoDao.updateByPrimaryKeySelective(posterPO);
    }

    @Override
    public ArticleInfoVO selectById(String id) {
        ArticleInfoPO posterPO=articleInfoDao.selectByPrimaryKey(id);
        if(posterPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        ArticleInfoVO articleInfoVO=new ArticleInfoVO();
        BeanUtils.copyProperties(posterPO, articleInfoVO);
        return articleInfoVO;
    }

    @Override
    public List<ArticleInfoVO> selectByList() {
        return articleInfoDao.selectByList();
    }
}